/** @format */
import Cookie from "js-cookie";
import axios from "axios";
const instance = axios.create({
  baseURL: "/api",
  timeout: 5000
  // withCredentials: true
});
// instance.get("https://m.flyco.com/b2c/init/0-0/init").then((res) => {
//   console.log(res);
// });
/* eslint-disable */
instance.interceptors.request.use(
  function(config) {
    config.headers.authorization = Cookie.get("token"); //拦截所有的请求，给所有的请求头上面加上请求然后发送过去。
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);
instance.interceptors.response.use(
  function(response) {
    return response; //拦截所有的请求，给所有的请求头上面加上请求然后发送过去。;
  },
  function(error) {
    if (error && error.message.indexOf("timeout") > -1) {
      alert({ type: "warning", message: "网络请求超时，检查一下你的网络呦~" });
    }
    return Promise.reject(error);
  }
);
/**
 * 发起get请求
 * @param {*} url
 * @param {*} params
 */
export const get = (url, params) =>
  instance.get(url, {
    params
  });

/**
 * 发送post请求
 * @param {*} url
 * @param {*} data
 */

export const post = (url, data) => instance.post(url, data);
//根据instance 实例化的axios 封装四个请求
const apost = (url, data) => instance.post(url, data);
const del = (url, params) => instance.delete(url, { params });
const put = (url, data) => instance.put(url, data);
//导出所有的请求
export { apost, del, put, instance };

